package com.ep.stepview.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.ep.stepview.R;

/**
 * Created by Administrator on 2017/7/11.
 */

public class CircleProgressBar extends View {
    private int mOutCircleColor;
    private int mInArcColor;
    private int mTextSize;
    private int mTextColor;
    private int mBorderWidth;

    private Paint mOutCirclePaint,mInArcPaint,mTextPaint;

    private int mMaxProgress=200;
    private float mCurProgress;

    public CircleProgressBar(Context context) {
        this(context,null);
    }

    public CircleProgressBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CircleProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.CircleProgressBar);
        mOutCircleColor=typedArray.getColor(R.styleable.CircleProgressBar_outCircliColor,mOutCircleColor);
        mInArcColor=typedArray.getColor(R.styleable.CircleProgressBar_inArcColor,mInArcColor);
        mTextColor=typedArray.getColor(R.styleable.CircleProgressBar_progressTextColor,mTextColor);
        mTextSize=typedArray.getDimensionPixelSize(R.styleable.CircleProgressBar_progressTextSize,mTextSize);
        mBorderWidth=typedArray.getDimensionPixelSize(R.styleable.CircleProgressBar_progressBorderWidth,mBorderWidth);
        typedArray.recycle();

        mOutCirclePaint=getColorPaint(mOutCircleColor);
        mInArcPaint=getColorPaint(mInArcColor);

        mTextPaint=new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(mTextSize);
        mTextPaint.setColor(mTextColor);
    }

    private Paint getColorPaint(int color) {
        Paint paint=new Paint();
        paint.setAntiAlias(true);
        paint.setColor(color);
        paint.setStrokeWidth(mBorderWidth);
        paint.setStyle(Paint.Style.STROKE);
        return paint;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width=MeasureSpec.getSize(widthMeasureSpec);
        int height=MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(width>=height?height:width,height>=width?width:height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //画圆
        int center=getWidth()/2;
        canvas.drawCircle(center,center,center-mBorderWidth/2,mOutCirclePaint);

        //画弧
        RectF oval=new RectF(mBorderWidth/2,mBorderWidth/2,getWidth()-mBorderWidth/2,getHeight()-mBorderWidth/2);
        float percent=mCurProgress/mMaxProgress;
        canvas.drawArc(oval,0,percent*360,false,mInArcPaint);

        //画文字
        String text=(int)(percent*100)+"%";

        Rect textBounds=new Rect();
        mTextPaint.getTextBounds(text,0,text.length(),textBounds);
        int x=getWidth()/2-textBounds.width()/2;
        Paint.FontMetricsInt fontMetricsInt=mTextPaint.getFontMetricsInt();
        int dy=(fontMetricsInt.bottom-fontMetricsInt.top)/2-fontMetricsInt.bottom;
        int baseLine=getHeight()/2+dy;
        canvas.drawText(text,x,baseLine,mTextPaint);
    }

    public void setCurProgress(float progress){
        this.mCurProgress=progress;
        invalidate();
    }
    public void setMaxProgress(int maxProgress){
        this.mMaxProgress=maxProgress;
    }
}
